package com.blacktech.dbu.meta.entity;

import com.blacktech.dbu.core.common.BaseEntity;
import com.blacktech.dbu.core.dict.ColumnType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 数据表字段信息
 * 现在支持从字段库中选择字段定义
 *
 * @author yanyunsong
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "dbu_column")
public class DbuColumn extends BaseEntity {

    /**
     * 数据表字段名称
     */
    @Column(name = "column_name", nullable = false)
    private String columnName;

    /**
     * 数据表字段注释
     */
    @Column(name = "column_comment")
    private String columnComment;

    /**
     * 数据表字段类型
     */
    @Column(name = "column_type")
    @Enumerated(EnumType.STRING)
    private ColumnType columnType;

    /**
     * 关联的数据表
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "table_id")
    @JsonIgnore
    private DbuTable table;

    /**
     * 字段长度
     * <p>
     * 用于字符型
     */
    @Column(name = "column_length")
    private Integer columnLength;

    /**
     * 字段精度
     * <p>
     * 用于数值型
     */
    @Column(name = "column_precision")
    private Integer columnPrecision;

    /**
     * 字段小数位数
     * <p>
     * 用于数值型
     */
    @Column(name = "column_scale")
    private Integer columnScale;

    /**
     * 是否从字段库中选择
     */
    @Column(name = "from_field_library", nullable = false)
    private Boolean fromFieldLibrary = false;

    /**
     * 关联的字段库ID（如果从字段库选择）
     */
    @Column(name = "field_library_id")
    private Long fieldLibraryId;

    /**
     * 默认值
     */
    @Column(name = "default_value", length = 200)
    private String defaultValue;

    /**
     * 是否必填
     */
    @Column(name = "is_required", nullable = false)
    private Boolean isRequired = false;

    /**
     * 是否主键
     */
    @Column(name = "is_primary_key", nullable = false)
    private Boolean isPrimaryKey = false;

    /**
     * 是否唯一
     */
    @Column(name = "is_unique", nullable = false)
    private Boolean isUnique = false;

    /**
     * 排序序号
     */
    @Column(name = "sort_order")
    private Integer sortOrder = 0;

    /**
     * 关联的字段库实体（懒加载）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "field_library_id", insertable = false, updatable = false)
    private DbuFieldLibrary fieldLibrary;
}